import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { AppService } from '../app.service';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {

  constructor(private router: Router ,private appService: AppService) { }
  
  login = true;
  showMessage = false;
  showResponse = false;

  registerForm = {
    name : '',
    password: '',
    confirmPassword: '',
    phone: ''
  };

  registerBtn = () => {
    if(this.registerForm.confirmPassword && this.registerForm.confirmPassword == this.registerForm.password) {
      this.appService.addUser({
        username: this.registerForm.name,
        password: this.registerForm.password,
        phone: this.registerForm.phone
      }).subscribe(item => console.log('addUser',item));
    } else {
      this.showMessage = true;
      setTimeout(()=>this.showMessage= false,1000)
    }
  }

  loginForm = {
    name : '',
    password: ''
  };

  loginBtn = () => {
    this.appService.getUser({
      username: this.loginForm.name,
      password: this.loginForm.password
    }).subscribe(res => {
      if(res['code'] == '1') {
        // 跳转
        this.router.navigate(['/main'])
      } else {
        this.showResponse = true;
        setTimeout(()=>this.showResponse=false,1000)
      }
    })
  }

  tabChange = (a) => {
    this.login = a;
  }

  ngOnInit(): void {
  }

}
